/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.KamarDAO;
import com.ivanbiz.dao.impl.KamarDAOImpl;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.model.Kos;
import com.ivanbiz.model.Penghuni;
import com.ivanbiz.service.JTextFieldLimit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class SewaKamarUpdatePanel extends javax.swing.JPanel {

    JDialog jDialog;
    Kamar kamar;
    Kos kos;
    Penghuni penghuni;

    public SewaKamarUpdatePanel(JDialog jDialog, Kamar kamar) {
        initComponents();
        this.jDialog = jDialog;
        this.kamar = kamar;
        textKos.setText(kamar.getKos().getNama());
        textKamar.setText(kamar.getKodeKamar());
        textTarif.setText(kamar.getTarif() == 0 ? "" : String.valueOf(new Double(kamar.getTarif()).intValue()));
        textPenghuni.setText(kamar.getPenghuni() == null ? "" : kamar.getPenghuni().getIdIdentitas());
        this.kos = kamar.getKos();
        this.penghuni = kamar.getPenghuni();
        labelKamar.setText("Ubah Kamar");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelKamar = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        textKos = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        textPenghuni = new javax.swing.JTextField();
        buttonCariPenghuni = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        textTarif = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        textKamar = new javax.swing.JTextField();
        buttonFasilitas = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        buttonSimpan = new javax.swing.JButton();
        buttonBatal = new javax.swing.JButton();

        labelKamar.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        labelKamar.setText("Tambah / Ubah Penghuni Kamar");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel8.setText("Kos :");

        textKos.setEditable(false);

        jLabel7.setText("Penghuni :");

        textPenghuni.setEditable(false);

        buttonCariPenghuni.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/pencarian.jpg"))); // NOI18N
        buttonCariPenghuni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCariPenghuniActionPerformed(evt);
            }
        });

        jLabel6.setText("Tarif :");

        textTarif.setDocument(new JTextFieldLimit(12));
        textTarif.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textTarifKeyReleased(evt);
            }
        });

        jLabel1.setText("No Kamar :");

        textKamar.setEditable(false);

        buttonFasilitas.setText("Fasilitas");
        buttonFasilitas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonFasilitasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(325, 325, 325))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(textTarif, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(textPenghuni)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonCariPenghuni))
                            .addComponent(textKos)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(textKamar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonFasilitas)))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel7))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textKos, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textKamar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonFasilitas))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textPenghuni, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCariPenghuni))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textTarif, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/simpan.jpg"))); // NOI18N
        buttonSimpan.setText("Simpan");
        buttonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSimpanActionPerformed(evt);
            }
        });
        jPanel2.add(buttonSimpan);

        buttonBatal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/batal.jpg"))); // NOI18N
        buttonBatal.setText("Batal");
        buttonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBatalActionPerformed(evt);
            }
        });
        jPanel2.add(buttonBatal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelKamar)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelKamar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCariPenghuniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCariPenghuniActionPerformed
        JDialog jDialogPenghuni = new JDialog();
        jDialogPenghuni.setModal(true);
        jDialogPenghuni.setSize(800, 600);
        PenghuniPanel penghuniPanel = new PenghuniPanel(jDialogPenghuni, "kamar");
        jDialogPenghuni.add(penghuniPanel);
        jDialogPenghuni.setLocationRelativeTo(null);
        jDialogPenghuni.setVisible(true);
        this.penghuni = penghuniPanel.getPenghuniKamar();
        textPenghuni.setText(penghuniPanel.getPenghuniKamar() == null ? null : penghuniPanel.getPenghuniKamar().getIdIdentitas());
    }//GEN-LAST:event_buttonCariPenghuniActionPerformed

    private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBatalActionPerformed
        jDialog.dispose();
    }//GEN-LAST:event_buttonBatalActionPerformed

    private void buttonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSimpanActionPerformed
        if (kamar == null) {
            kamar = new Kamar();
        }
        kamar.setKos(kos);
        kamar.setKodeKamar(textKamar.getText());
        kamar.setTarif(textTarif.getText().isEmpty() ? (double) 0 : new Double(textTarif.getText()));
        kamar.setPenghuni(penghuni == null ? null : penghuni);
        validate(kamar);
    }//GEN-LAST:event_buttonSimpanActionPerformed

    private void textTarifKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textTarifKeyReleased
        try {
            Long.parseLong(textTarif.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Format salah, harus angka", "warning", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_textTarifKeyReleased

    private void buttonFasilitasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonFasilitasActionPerformed
        JDialog jDialogFasilitas = new JDialog();
        jDialogFasilitas.setModal(true);
        jDialogFasilitas.setSize(600, 400);
        jDialogFasilitas.add(new FasilitasPanel(kamar));
        jDialogFasilitas.setLocationRelativeTo(null);
        jDialogFasilitas.setVisible(true);
    }//GEN-LAST:event_buttonFasilitasActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBatal;
    private javax.swing.JButton buttonCariPenghuni;
    private javax.swing.JButton buttonFasilitas;
    private javax.swing.JButton buttonSimpan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel labelKamar;
    private javax.swing.JTextField textKamar;
    private javax.swing.JTextField textKos;
    private javax.swing.JTextField textPenghuni;
    private javax.swing.JTextField textTarif;
    // End of variables declaration//GEN-END:variables

    private void validate(Kamar kamar) {
        if (kamar.getPenghuni() == null) {
            JOptionPane.showMessageDialog(this, "Penghuni tidak boleh kosong");
        } else if (kamar.getTarif() == 0) {
            JOptionPane.showMessageDialog(this, "Tarif tidak boleh kosong");
        } else {
            try {
                KamarDAO kamarDAO = new KamarDAOImpl();
                kamarDAO.update(kamar);
                jDialog.dispose();
            } catch (Exception ex) {
                Logger.getLogger(SewaKamarUpdatePanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
